/**
 * 弹窗
 */
define([
    'require'
], function(require) {
    'use strict';
    /**
     * 弹窗类
     * @class
     * @param {*} parent 包含弹窗的元素 
     * @param {*} top 弹窗距离parent顶部的像素
     * @param {*} left 弹窗距离parent右侧的像素
     */
    function Popup(id ,parent, name, top){
        if(typeof Popup.popups[id] !== 'undefined'){
            // 重新调整位置
            var popup = Popup.popups[id];
            popup._top = top;
            popup._left = (parent.clientWidth-500) / 2;
            popup._element.style = 'top:'+popup._top+'px;left:'+popup._left+'px';
            return popup;
        }
        this._parent = parent;
        this._element = document.createElement('div');
        this._inputs = new Array();
        this._top = top;
        this._left = (parent.clientWidth-500) / 2;
        this._name = name;
        this._locked = false;   // 弹窗锁住标志,若为true则不能向弹窗添加新内容
        this._init();
        // 加入缓存
        Popup.popups[id] = this;
    }
    // 弹窗缓存
    Popup.popups = {};
    Object.defineProperty(Popup.prototype, 'locked', {
        get : function(){
            return this._locked;
        }
    })
    /**
     * 初始化弹窗
     */
    Popup.prototype._init = function(){
        var element = this._element;
        this._parent.appendChild(element);
        // 设置弹窗属性
        element.style = 'top:'+this._top+'px;left:'+this._left+'px;display:none;';

        // 添加标题
        var title = document.createElement('h3');
        title.innerText = this._name;
        element.appendChild(title);
        element.classList.add('popup');
        
        // 关闭弹窗按钮
        var close = document.createElement('span');
        close.classList.add('popup-close');
        var that = this;
        close.addEventListener('click', function() {
            that.close();
        })
        element.appendChild(close);
        
    }
    /**
     * 显示弹窗
     */
    Popup.prototype.show = function(){
        this._element.style.display = 'block';
    }
    /**
     * 关闭弹窗
     */
    Popup.prototype.close = function(){
        this._element.style.display = 'none';
    }
    /**
     * 清除弹窗内容
     */
    Popup.prototype.clear = function(){
        this._element.innerHTML = '';
        this._inputs.clear();
        this._locked = false;
    }
    /**
     * 添加输入框
     * @param {} info 提示信息
     * @param {} name 输入框name
     * @param {} placeHolder placeHolder
     */
    Popup.prototype.addInput = function(info, name, placeHolder){
        if(this._locked){
            return
        }
        var ele = document.createElement('div');
        ele.classList.add('popup-item');
        ele.innerHTML = '<span>'+info+'</span>';
        var input = document.createElement('input');
        input.type = 'text';
        input.name = name;
        input.value = '';
        input.placeholder = placeHolder || '';
        ele.appendChild(input);
        this._element.appendChild(ele);
        this._inputs.push(input);
    }
    /**
     * 添加下拉框
     * @param {} info 提示信息
     * @param {} name 下拉框name
     * @param {Array} selects 下拉框选项 [{name:name, value:value}]
     */
    Popup.prototype.addSelect = function(info, name, selects){
        if(this._locked){
            return
        }
        var ele = document.createElement('div');
        ele.classList.add('popup-item');
        ele.innerHTML = '<span>'+info+'</span>';

        // 创建select
        var selectElement = document.createElement('select');
        selectElement.name = name;
        for(var i=0; i<selects.length;i++){
            selectElement.innerHTML += '<option value='+selects[i].value+'>'+selects[i].name+'</option>';
        }
        ele.appendChild(selectElement);
        this._element.appendChild(ele);
        this._inputs.push(selectElement);
    }
    /**
     * 添加一段文本
     * @param {*} text 文本
     */
    Popup.prototype.addText = function(text){
        if(this._locked){
            return
        }
        var ele = document.createElement('div');
        ele.classList.add('popup-item');
        ele.innerText = text;
        this._element.appendChild(ele);
    }
    /**
     * 添加一个按钮
     * @param {*} info 按钮信息 
     * @param {*} callBack 点击按钮触发的回调
     */
    Popup.prototype.addButton = function(info, callBack){
        if(this._locked){
            return
        }
        var ele = document.createElement('div');
        ele.classList.add('popup-item');
        ele.classList.add('popup-item-btns');
        if(Array.isArray(info)){
            for(var i=0;i<info.length;i++){
                var input = document.createElement('input');
                input.type = 'button';
                input.value = info[i];
                input.addEventListener('click', callBack[i]);
                ele.appendChild(input);                
            }
        }else{
            ele.innerHTML += '<input type=\'button\' value='+info+'>';
            ele.addEventListener('click', callBack);
        }
        this._element.appendChild(ele);
    }
    /**
     * 添加一个滑动条
     * @param {*} info 提示信息
     * @param {*} name range name
     * @param {*} min 最小值
     * @param {*} max 最大值
     */
    Popup.prototype.addRange = function(info, name, min, max){
        if(this._locked){
            return
        }
        var ele = document.createElement('div');
        ele.classList.add('popup-item');
        ele.innerHTML = '<span>'+info+'</span>';
        var input = document.createElement('input');
        input.name = name;
        input.value = '';
        input.type = 'range';
        input.min = min;
        input.max = max;
        ele.appendChild(input);
        this._element.appendChild(ele);
        this._inputs.push(input);
    }
    /**
     * 获取所有input的{name:value}对
     */
    Popup.prototype.getInputsValue = function(){
        var inputs = this._inputs;
        var values = new Object();
        for(var i=0;i<inputs.length;i++){
            values[inputs[i].name] = inputs[i].value;
        }
        return values;
    }
    /**
     * 清空所有输入框
     */
    Popup.prototype.clearInputs = function(){
        var inputs = this._inputs;
        for(var i=0;i<inputs.length;i++){
            inputs[i].value = '';
        }
    }
    /**
     * 锁弹窗
     */
    Popup.prototype.lock = function(){
        this._locked = true;
    }
    /**
     * 解锁弹窗
     */
    Popup.prototype.unlock = function(){
        this._locked = false;
    }
    return Popup;
});